package com.huopaolan.yohuoshop.Mvp.View.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.design.widget.TabLayout.OnTabSelectedListener;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

import com.huopaolan.yohuoshop.Mvp.View.Activity.HomeActivity;
import com.huopaolan.yohuoshop.R;

public class CommunityFragment extends Fragment implements OnTabSelectedListener, OnClickListener {
    private TabLayout tabLayout;
    private Fragment currentPosistion;
    private StrollFragment strollFragment;
    private StrollCommunityFragment strollCommunityFragment;
    private ShowFragment showFragment;
    private ImageView head_image;
    private ImageView imageView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_community, container, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {
        tabLayout = inflate.findViewById(R.id.frgment_community_head_tl);
        head_image = inflate.findViewById(R.id.frgment_community_head_right);
        imageView = inflate.findViewById(R.id.frgment_community_head_menu);
        imageView.setOnClickListener(this);
        strollFragment = new StrollFragment();
        strollCommunityFragment = new StrollCommunityFragment();
        showFragment = new ShowFragment();
        tabLayout.addTab(tabLayout.newTab().setText("逛"));
        tabLayout.addTab(tabLayout.newTab().setText("社区"));
        tabLayout.addTab(tabLayout.newTab().setText("ShOw"));
        tabLayout.setOnTabSelectedListener(this);
        FragmentSelect(strollFragment);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        boolean stroll = tab.getText().equals("逛");
        boolean community = tab.getText().equals("社区");
        boolean show = tab.getText().equals("ShOw");
        if (stroll) {
            head_image.setImageResource(R.drawable.ic_heart_fill);
            FragmentSelect(strollFragment);
        } else if (community) {
            head_image.setImageResource(R.drawable.community_write);
            FragmentSelect(strollCommunityFragment);
        } else if (show) {
            head_image.setImageResource(R.drawable.ic_heart_fill);
            FragmentSelect(showFragment);
        }
    }

    private void FragmentSelect(Fragment fragment) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if (currentPosistion != fragment) {
            if (currentPosistion != null && currentPosistion.isAdded()) {
                transaction.hide(currentPosistion);
            }
            if (!fragment.isAdded()) {
                transaction.add(R.id.frgment_community_fl, fragment, "");
                transaction.show(fragment);
                transaction.commit();
            } else {
                transaction.show(fragment);
                transaction.commit();
            }
            currentPosistion = fragment;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onStart() {
        Window window = getActivity().getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.frgment_community_head_menu:
                HomeActivity.slidingMenu.showMenu();
                break;
        }
    }
}
